package com.shujia.mr.clazzSort2;


import org.apache.hadoop.io.Writable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

class StudentMap implements Writable {
    String id;
    String name;
    int age;
    String gender;
    String clazz;
    int score;

    // java.lang.NoSuchMethodException: com.shujia.mr.clazzSort2.StudentMap.<init>()
    //   init表示实例化
    public StudentMap() {
    }

    public StudentMap(String id, String name, int age, String gender, String clazz, int score) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.gender = gender;
        this.clazz = clazz;
        this.score = score;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", gender='" + gender + '\'' +
                ", clazz='" + clazz + '\'' +
                ", score=" + score +
                '}';
    }


    /*
        对数据写出是序列化过程
     */
    @Override
    public void write(DataOutput out) throws IOException {
        /*
        String id;
    String name;
    int age;
    String gender;
    String clazz;
    int score;
         */
        out.writeUTF(this.id); // HADOOP中UTF 表示的就是一个字符串
        out.writeUTF(this.name);
        out.writeInt(this.age);
        out.writeUTF(this.gender);
        out.writeUTF(this.clazz);
        out.writeInt(this.score);
    }

    /*
        read 表示反序列化，其顺序需要和序列化顺序一致
     */
    @Override
    public void readFields(DataInput in) throws IOException {
        this.id = in.readUTF();
        this.name = in.readUTF();
        this.age = in.readInt();
        this.gender = in.readUTF();
        this.clazz = in.readUTF();
        this.score = in.readInt();
    }

}
